import DestructibleFacility from "../facility/DestructibleFacility";
import MoveComp from "../funcComp/MoveComp";
import HealthPoint from "../funcObj/interface/HealthPoint";
import { FlyerType } from "../info/GameEnum";
import { Global } from "../info/Global";
import Player from "../player/Player";
import { Location } from "../info/Predefine";

const { ccclass, property } = cc._decorator;

/**
 * 所有飞行物的基类
 */
@ccclass
export default abstract class Flyer extends cc.Component {

    public static readonly EventType = {
        摧毁: "摧毁",
    }

    @property({
        type: cc.Enum(FlyerType)
    })
    flyerType: FlyerType = FlyerType.侦察机;

    /**
     *移动时是否获得视野;
     */
    @property(cc.Boolean)
    monitor: boolean = true;

    moveComp: MoveComp = null;

    /**
     * 操控的玩家
     */
    player: Player;

    /**
     * 基地
     */
    base: DestructibleFacility;

    /**
     * 飞行物信息
     */
    flyerInfo: any;

    init(base: DestructibleFacility, target?: HealthPoint | cc.Vec3 | Location) {
        this.player = base.player;
        this.base = base;
        this.flyerInfo = Global.getFlyerInfo<{ 移动速度: number }>(this.flyerType);

        this.base.node.on(DestructibleFacility.EventType.摧毁, this.onBaseDestory, this);

        this.moveComp = this.node.getComponent(MoveComp);
        this.moveComp.init(this.player, this.monitor, this.flyerInfo.移动速度);
        //如果初始时传入目标，立即出发
        if (target) {
            this.dispatch(target);
        }
    }

    public abstract dispatch(target: HealthPoint | cc.Vec3 | Location);

    /**
     * 当基地被摧毁
     * @param killer 
     */
    onBaseDestory() {
        if (!this.base)
            cc.log("error");
        this.base.node.off(DestructibleFacility.EventType.摧毁, this.onBaseDestory, this); 
        this.base = null;
    }

    /**
     * 飞行物抵达目的地
     */
    onArrive() {
        this.base && this.onBaseDestory();
    };
}
